package com.kanshu.ksgb.fastread.doudou.module.bookcity.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;

/**
 * Created by wcy on 18/6/12.
 */

public class LeanIndicatorContainer extends LinearLayout {
    public LeanIndicatorContainer(Context context) {
        super(context);
        init();
    }

    public LeanIndicatorContainer(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LeanIndicatorContainer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(HORIZONTAL);
    }

    public void createIndicators(int num) {
        removeAllViews();
        int height = (num - 1) * DisplayUtils.dip2px(getContext(), 2);
        for (int i = 0; i < num; i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item_banner_dot_layout, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.container);
            int marginLeft = DisplayUtils.dip2px(getContext(), 13);
            if (i == 0) {
                marginLeft = 0;
            }
            params.height = height + DisplayUtils.dip2px(getContext(), 7);
            params.leftMargin = marginLeft;
            int marginBottom = i * DisplayUtils.dip2px(getContext(), 2);
            frameLayout.setPadding(0, 0, 0, marginBottom);
            addView(view, params);
        }
    }

    public void setSelected(int pos) {
        if (pos >= getChildCount()) {
            return;
        }
        for (int i = 0; i < getChildCount(); i++) {
            FrameLayout frameLayout = (FrameLayout) getChildAt(i);
            ImageView imageView = (ImageView) frameLayout.getChildAt(0);
            imageView.setImageResource(i == pos ? R.mipmap.ic_banner_dot_selected : R.mipmap.ic_banner_dot_unselected);
        }
    }

    private void createIndicator(int pos) {
        ImageView imageView = new ImageView(getContext());
        imageView.setImageResource(R.mipmap.ic_banner_dot_unselected);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        int marginLeft = DisplayUtils.dip2px(getContext(), 13);
        if (pos == 0) {
            marginLeft = 0;
        }
        int marginBottom = pos * DisplayUtils.dip2px(getContext(), 12);
        imageView.setPadding(0, 0, 0, marginBottom);
        params.leftMargin = marginLeft;
        addView(imageView, params);
    }
}
